<?php
/**
 * 新浪微博应用API调用接口类
 * @author shallker.wang@profero.com
 * @date  2012.07.31
 */
require_once APPPATH.'libraries/sina_weibo/app_config.php';
require_once APPPATH.'libraries/sina_weibo/saetv2.ex.class.php';

class Weibo_client {
    
    public $uid;
    public $user;
    
    var $app_key;
    var $app_secret_key;
    var $app_callback;
    
    var $access_token;
    var $client;
    
    public function __construct($access_token_arr) {
        $this->access_token = current($access_token_arr);
        $this->app_key = WB_AKEY;
        $this->app_secret_key = WB_SKEY;
        $this->app_callback = WB_CALLBACK_URL;
        $this->client = new SaeTClientV2($this->app_key, $this->app_secret_key, $this->access_token);
        $this->check_validness();
    }
    
    /*
     * @desc 检测oAuth服务器端是否正正常有效
     * @return null
     */
    protected function check_validness() {
        $uid = $this->client->get_uid();
        if (isset($uid['error'])) {
            throw new Exception($uid['error']);
        }
        $user = $this->client->show_user_by_id($uid['uid']);
        if (isset($user['error'])) {
            throw new Exception($user['error']);
        }
        $this->user = $user;
        $this->uid = $uid['uid'];
    }
    
    /*
     * @desc oAuth授权后, 获取用户的uid
     * @return array('uid'=>21314)
     */
    public function get_uid() {
        return current($this->client->get_uid());
    }

    /*
     * @desc 获取某个用户的信息
     * @params $uid 用户id
     * @return arr 数组信息
     *  
     */
    public function get_user_info($uid=null) {
        $uid = $uid ? $uid : $this->uid;
        return $this->client->show_user_by_id($uid);
    }
    
    /*
     * @desc 分享一条微博
     * @params $words 分享的文字
     * @return arr 此条微博的状态信息
     */
    public function share($words) {
        return $this->client->update($words);
    }
    
    /*
     * @desc 分享一条带图片的微博
     * @params $words 分享的文字 $img_url 分享的图片URL地址
     * @return 
     */
    public function share_image($words, $img_url) {
        return $this->client->upload($words, $img_url);
    }
    
    /*
     * @desc 转发一条微博
     * @params $sid 要转发微博的id
     * @return
     */
    public function reshare($sid) {
        return $this->client->repost_timeline($sid);
    }
    
    /*
     * @desc 关注某个用户
     * @params $uid 关注用户的id
     * @return arr 被关注用户的信息
     */
    public function follow($uid) {
        return $this->client->follow_by_id($uid);
    }
    
    /*
     * @desc 取消关注某个用户
     * @params $uid 取消关注用户的id
     * @return 
     */
    public function unfollow($uid) {
        return $this->client->unfollow_by_id($uid);
    }
    
    /*
     * @desc 获取用户的互相关注人信息
     * @param int $uid 用户id
     * @param int $page
     * @param int $count 默认50
     * @param int $sort
     * @return 
     */
    public function get_mutual_follows($uid, $page=1, $count=50, $sort=0) {
        return $this->client->bilateral($uid, $page, $count, $sort);
    }
    
    /*
     * @desc 获取用户的互相关注人id
     * @param int $uid 用户id
     * @param int $page
     * @param int $count 默认50 最大2000
     * @param int $sort
     * @return 
     */
    public function get_mutual_follows_ids($uid, $page=1, $count=50, $sort=0) {
        return $this->client->bilateral_ids($uid, $page, $count, $sort);
    }
    
    /*
     * @desc 获取用户的关注人信息
     * @param int $uid 用户id
     * @param int $cursor 游标
     * @param int $count 默认50 最大200
     * @return 
     */
    public function get_follows($uid, $cursor=0, $count=50) {
        return $this->client->friends_by_id($uid, $cursor, $count);
    }
    
    /*
     * @desc 获取用户的关注人id
     * @param int $uid 用户id
     * @param int $cursor 游标
     * @param int $count 默认500 最大5000
     * @return 
     */
    public function get_follows_ids($uid, $cursor=0, $count=500) {
        return $this->client->friends_ids_by_id($uid, $cursor, $count);
    }
    
    /*
     * @desc 获取用户粉丝的信息
     * @param int $uid 用户id
     * @param int $cursor 游标
     * @param int $count 默认50 最大200
     * @return 
     */
    public function get_followers($uid, $cursor=0, $count=50) {
        return $this->client->followers_by_id($uid, $cursor, $count);
    }
    
    /*
     * @desc 获取用户粉丝的id
     * @param int $uid 用户id
     * @param int $cursor 游标
     * @param int $count 默认500 最大5000
     * @return 
     */
    public function get_followers_ids($uid, $cursor=0, $count=500) {
        return $this->client->followers_ids_by_id($uid, $cursor, $count);
    }
    
    public function __destruct() {}
}
